// QQ表情插件
(function($){
    $.fn.qqFace = function(options){
        var defaults = {
            id : 'facebox',
            path : 'face/',
            assign : 'content',
            tip : 'em_'
        };
        var option = $.extend(defaults, options);
        var assign = $('#'+option.assign);
        var id = option.id;
        var path = option.path;
        var tip = option.tip;
        if(assign.length<=0){
            // alert('缺少表情赋值对象。');
            return false;
        }
        $(this).click(function(e){
            var strFace, labFace;
            if($('#'+id).length<=0){
                strFace = '<div id="'+id+'" style="position:absolute;display:none;z-index:1000;background:#F0FFF0" class="qqFace">' +
                    '<table border="0" cellspacing="0" cellpadding="0"  bgcolor="#F0FFF0"><tr>';
                var icons = [
                    'Hui-iconfont-home','Hui-iconfont-home2','Hui-iconfont-news','Hui-iconfont-tuku','Hui-iconfont-music','Hui-iconfont-tags','Hui-iconfont-yuyin3','Hui-iconfont-system','Hui-iconfont-help','Hui-iconfont-chuku',
                    'Hui-iconfont-picture','Hui-iconfont-fenlei','Hui-iconfont-hetong','Hui-iconfont-quanbudingdan','Hui-iconfont-renwu','Hui-iconfont-feedback','Hui-iconfont-feedback2','Hui-iconfont-dangan','Hui-iconfont-log','Hui-iconfont-pages',
                    'Hui-iconfont-file','Hui-iconfont-manage2','Hui-iconfont-order','Hui-iconfont-yuyin2','Hui-iconfont-yuyin','Hui-iconfont-picture1','Hui-iconfont-tuwenxiangqing','Hui-iconfont-moban-2','Hui-iconfont-jieri','Hui-iconfont-moban',
                    'Hui-iconfont-clock'
                ];
                for(var i=1; i<=icons.length; i++){
                    labFace = icons[i-1];
                    strFace += '<td><i class="Hui-iconfont '+icons[i-1]+'" onclick="$(\'#'+option.assign+'\').setCaret();$(\'#'+option.assign+'\').insertAtCaret(\'' + labFace + '\');" /></td>';
                    if( i % 15 == 0 ) strFace += '</tr><tr>';
                }
                strFace += '</tr></table></div>';
            }
            $(this).parent().append(strFace);
            var offset = $(this).position();
            var top = offset.top + $(this).outerHeight();
            $('#'+id).css('top',top);
            $('#'+id).css('left',offset.left);
            $('#'+id).show();
            e.stopPropagation();
        });

        $(document).click(function(){
            $('#'+id).hide();
            $('#'+id).remove();
        });
    };

})(jQuery);

jQuery.extend({
    unselectContents: function(){
        if(window.getSelection)
            window.getSelection().removeAllRanges();
        else if(document.selection)
            document.selection.empty();
    }
});
jQuery.fn.extend({
    selectContents: function(){
        $(this).each(function(i){
            var node = this;
            var selection, range, doc, win;
            if ((doc = node.ownerDocument) && (win = doc.defaultView) && typeof win.getSelection != 'undefined' && typeof doc.createRange != 'undefined' && (selection = window.getSelection()) && typeof selection.removeAllRanges != 'undefined'){
                range = doc.createRange();
                range.selectNode(node);
                if(i == 0){
                    selection.removeAllRanges();
                }
                selection.addRange(range);
            } else if (document.body && typeof document.body.createTextRange != 'undefined' && (range = document.body.createTextRange())){
                range.moveToElementText(node);
                range.select();
            }
        });
    },

    setCaret: function(){
        if(!$.browser.msie) return;
        var initSetCaret = function(){
            var textObj = $(this).get(0);
            textObj.caretPos = document.selection.createRange().duplicate();
        };
        $(this).click(initSetCaret).select(initSetCaret).keyup(initSetCaret);
    },

    insertAtCaret: function(textFeildValue){
        var textObj = $(this).get(0);
        // if(document.all && textObj.createTextRange && textObj.caretPos){
        // 	var caretPos=textObj.caretPos;
        // 	caretPos.text = caretPos.text.charAt(caretPos.text.length-1) == '' ?
        // 	textFeildValue+'' : textFeildValue;
        // } else if(textObj.setSelectionRange){
        // 	alert('aaa');
        // 	var rangeStart=textObj.selectionStart;
        // 	var rangeEnd=textObj.selectionEnd;
        // 	var tempStr1=textObj.value.substring(0,rangeStart);
        // 	var tempStr2=textObj.value.substring(rangeEnd);
        // 	textObj.value=tempStr1+textFeildValue+tempStr2;
        // 	textObj.focus();
        // 	var len=textFeildValue.length;
        // 	textObj.setSelectionRange(rangeStart+len,rangeStart+len);
        // 	textObj.blur();
        // }else{
        // textObj.value+=textFeildValue;
        textObj.value=textFeildValue;
        // }
    }
});